//
//  AnimationViewController.m
//  newTestGit
//
//  Created by fangjs on 16/3/10.
//  Copyright © 2016年 Hongshi. All rights reserved.
//

#import "AnimationViewController.h"
#import "ButtonAnimationModel.h"
#import "TheCustomButton.h"
#import "NavigationRouteViewController.h"

#define keyPath(obj,keyPath) @(((void)obj.keyPath,#keyPath))

@interface AnimationViewController ()

@property (nonatomic,strong) NSMutableArray *btnItems;
@property (nonatomic , strong) NSTimer *timer;
@property (nonatomic , assign) NSInteger btnIndex;
@property (assign) BOOL mark;

@property (nonatomic , strong) UIView *mainV;

@end

@implementation AnimationViewController

- (NSMutableArray *)btnItems {
    if (_btnItems == nil) {
        _btnItems = [NSMutableArray array];
    }
    return _btnItems;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    NSLog(@"%@",keyPath(_mainV, frame));
    
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = item;
    
    [self createAnimationButton];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeChanged) userInfo:nil repeats:YES];
}

- (void) viewDidDisappear:(BOOL)animated {
        [self.btnItems enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton *btn = obj;
            btn.alpha = 1;
            btn.transform = CGAffineTransformMakeScale(1.0, 1.0);
        }];
}

- (void) backAction {
    _mark = YES;
    [_timer setFireDate:[NSDate distantPast]];//open timer
}

-(void) timeChanged {

   static int count = 0;
    NSLog(@"%d",count);
    if (!_mark) {
        if (_btnIndex == _btnItems.count) {
            _btnIndex = 5;
            [_timer setFireDate:[NSDate distantFuture]];//close timer
            return;
        }
        UIButton *btn = _btnItems[_btnIndex];
        [self animationStart:btn];
        _btnIndex++;
    }
    else {
         if (_btnIndex < 0) {
             _mark = NO;
             _btnIndex = 0;
            [_timer invalidate];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        UIButton *btn = _btnItems[_btnIndex];
        [self animationEnd:btn];
        _btnIndex--;
    }
    count++;
}

- (void) animationStart:(UIButton *) btn {

    [UIView animateWithDuration:0.7 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
         btn.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {

    }];
}

- (void) animationEnd:(UIButton *) btn {
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseIn animations:^{
        btn.transform = CGAffineTransformMakeTranslation(0, self.view.bounds.size.height);
    } completion:^(BOOL finished) {

    }];
}

- (void) createAnimationButton {
    
    int cols = 3;
    int row = 0;
    int col = 0;
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat wh = 100;
    CGFloat margin = ([UIScreen mainScreen].bounds.size.width - cols * wh) / (cols + 1);
    CGFloat distanceY = 300;
    for (int i = 0; i < _buttonItems.count; i++) {
        UIButton *btn = [TheCustomButton buttonWithType:UIButtonTypeCustom];
        
        col = i % cols;
        row = i / cols;
        
        x = margin + col * (margin + wh);
        y = distanceY + row * (margin + wh);
        
        btn.frame = CGRectMake(x, y, wh, wh);
        
        ButtonAnimationModel *btnModel = _buttonItems[i];
        
        [btn setImage:btnModel.image forState:UIControlStateNormal];
        [btn setTitle:btnModel.title forState:UIControlStateNormal];
        
        btn.transform = CGAffineTransformMakeTranslation(0, self.view.bounds.size.height);
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
        [btn addTarget:self action:@selector(btnClick1:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [self.btnItems addObject:btn];
        [self.view addSubview:btn];
    }
}


- (void) btnClick:(UIButton *) btn {
    
    [UIView animateWithDuration:0.5 animations:^{
        btn.transform = CGAffineTransformMakeScale(1.2, 1.2);
    }];
}

- (void) btnClick1:(UIButton *) btn  {
    
    [UIView animateWithDuration:0.5 animations:^{
        btn.transform = CGAffineTransformMakeScale(2.0, 2.0);
        btn.alpha = 0;
    } completion:^(BOOL finished) {
        NavigationRouteViewController *navVC = [[NavigationRouteViewController alloc] init];
        [self.navigationController pushViewController:navVC animated:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
